<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.2-20121118"/>
<title>SensorFramework: src/saxion/sensorframework/db/conn/SqlConnect.java Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td style="padding-left: 0.5em;">
   <div id="projectname">SensorFramework
   &#160;<span id="projectnumber">1</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.2-20121118 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="namespaces.html"><span>Packages</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
    </ul>
  </div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Pages</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_4c90d63c55d0a4a7ceb1e868f259078c.html">src</a></li><li class="navelem"><a class="el" href="dir_114c386281b70752c2da546ca7af09c9.html">saxion</a></li><li class="navelem"><a class="el" href="dir_55fa4d4c3ec59703226079a577bba96c.html">sensorframework</a></li><li class="navelem"><a class="el" href="dir_053f28733de37eeb0f8a52bf0788b052.html">db</a></li><li class="navelem"><a class="el" href="dir_5db45a7ded3edeabef95b8743ea62928.html">conn</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">SqlConnect.java</div>  </div>
</div><!--header-->
<div class="contents">
<a href="a00017.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"><a class="code" href="a00034.html">    1</a></span>&#160;<span class="keyword">package </span>saxion.sensorframework.db.conn;</div>
<div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;</div>
<div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;<span class="keyword">import</span> java.io.FileInputStream;</div>
<div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;<span class="keyword">import</span> java.sql.*;</div>
<div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;<span class="keyword">import</span> java.util.Properties;</div>
<div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;</div>
<div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;<span class="keyword">import</span> org.apache.log4j.Logger;</div>
<div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;</div>
<div class="line"><a name="l00017"></a><span class="lineno"><a class="code" href="a00012.html">   17</a></span>&#160;<span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">class </span><a class="code" href="a00012.html" title="SQL Class used to connect to the database and to execute queries.">SqlConnect</a> {</div>
<div class="line"><a name="l00018"></a><span class="lineno"><a class="code" href="a00012.html#a1b54b28524696aa33252c2d3981c3815">   18</a></span>&#160;    <span class="keyword">static</span> Logger <a class="code" href="a00012.html#a1b54b28524696aa33252c2d3981c3815">logger</a> = Logger.getLogger(<a class="code" href="a00012.html" title="SQL Class used to connect to the database and to execute queries.">SqlConnect</a>.class.getName());</div>
<div class="line"><a name="l00024"></a><span class="lineno"><a class="code" href="a00012.html#af438e4318b3b86a14500f1a235da6116">   24</a></span>&#160;    <span class="keyword">static</span> <span class="keyword">final</span> String <a class="code" href="a00012.html#af438e4318b3b86a14500f1a235da6116" title="&lt; log4j used for logging.">CONFIG_FILE</a> = <span class="stringliteral">&quot;config.properties&quot;</span>;</div>
<div class="line"><a name="l00027"></a><span class="lineno"><a class="code" href="a00012.html#a14905a855dd0404405bccfa453a27fd2">   27</a></span>&#160;    <span class="keyword">public</span> Connection <a class="code" href="a00012.html#a14905a855dd0404405bccfa453a27fd2" title="&lt; Location of project properties file">conn</a>;</div>
<div class="line"><a name="l00029"></a><span class="lineno"><a class="code" href="a00012.html#a22891c0396ec494ad8184bfa66dc0c5a">   29</a></span>&#160;    <span class="keyword">private</span> PreparedStatement <a class="code" href="a00012.html#a22891c0396ec494ad8184bfa66dc0c5a" title="&lt; The connection to the database">statement</a>;</div>
<div class="line"><a name="l00031"></a><span class="lineno"><a class="code" href="a00012.html#ada1f04b176cbc918263439d50e55295a">   31</a></span>&#160;    <span class="keyword">public</span> <span class="keyword">static</span> <a class="code" href="a00012.html" title="SQL Class used to connect to the database and to execute queries.">SqlConnect</a> <a class="code" href="a00012.html#ada1f04b176cbc918263439d50e55295a" title="&lt; Prepared statement that will be used to execute on the database">db</a>;</div>
<div class="line"><a name="l00034"></a><span class="lineno"><a class="code" href="a00012.html#a45f4ebd14354ea2e599f17cfd307046f">   34</a></span>&#160;    <span class="keyword">private</span> String <a class="code" href="a00012.html#a45f4ebd14354ea2e599f17cfd307046f" title="&lt; An instance of the class SqlConnect">url</a>;</div>
<div class="line"><a name="l00036"></a><span class="lineno"><a class="code" href="a00012.html#a01274e0961ae50c2a4f4498882e3d7eb">   36</a></span>&#160;    <span class="keyword">private</span> String <a class="code" href="a00012.html#a01274e0961ae50c2a4f4498882e3d7eb" title="&lt; Url to the database">dbName</a>;</div>
<div class="line"><a name="l00038"></a><span class="lineno"><a class="code" href="a00012.html#adcb721f63e93506c637a09aac3ac6df6">   38</a></span>&#160;    <span class="keyword">private</span> String <a class="code" href="a00012.html#adcb721f63e93506c637a09aac3ac6df6" title="&lt; Name of the database">driver</a>;</div>
<div class="line"><a name="l00040"></a><span class="lineno"><a class="code" href="a00012.html#a9866bb45cacbe65df40aa02f9cfe8329">   40</a></span>&#160;    <span class="keyword">private</span> String <a class="code" href="a00012.html#a9866bb45cacbe65df40aa02f9cfe8329" title="&lt; Java jdbc driver">username</a>;</div>
<div class="line"><a name="l00042"></a><span class="lineno"><a class="code" href="a00012.html#add82e06e9083c528e3f1046f3cdfd942">   42</a></span>&#160;    <span class="keyword">private</span> String <a class="code" href="a00012.html#add82e06e9083c528e3f1046f3cdfd942" title="&lt; Username wich is used on the database">password</a>;</div>
<div class="line"><a name="l00043"></a><span class="lineno">   43</span>&#160;</div>
<div class="line"><a name="l00054"></a><span class="lineno"><a class="code" href="a00012.html#a794ac61e7345eb264f667b269be0faf0">   54</a></span>&#160;    <span class="keyword">private</span> <a class="code" href="a00012.html#a794ac61e7345eb264f667b269be0faf0" title="&lt; Password wich is used on the database">SqlConnect</a>() throws SQLException, InstantiationException,</div>
<div class="line"><a name="l00055"></a><span class="lineno">   55</span>&#160;            IllegalAccessException, ClassNotFoundException {</div>
<div class="line"><a name="l00056"></a><span class="lineno">   56</span>&#160;</div>
<div class="line"><a name="l00057"></a><span class="lineno">   57</span>&#160;        <a class="code" href="a00012.html#aee72809b506fbf549bf30c8093b2731d" title="Reads the config.properties wich contains the information for the database.">readConfigFile</a>();</div>
<div class="line"><a name="l00058"></a><span class="lineno">   58</span>&#160;</div>
<div class="line"><a name="l00059"></a><span class="lineno">   59</span>&#160;        Class.forName(<a class="code" href="a00012.html#adcb721f63e93506c637a09aac3ac6df6" title="&lt; Name of the database">driver</a>).newInstance();</div>
<div class="line"><a name="l00060"></a><span class="lineno">   60</span>&#160;        this.<a class="code" href="a00012.html#a14905a855dd0404405bccfa453a27fd2" title="&lt; Location of project properties file">conn</a> = (Connection) DriverManager.getConnection(<a class="code" href="a00012.html#a45f4ebd14354ea2e599f17cfd307046f" title="&lt; An instance of the class SqlConnect">url</a> + <a class="code" href="a00012.html#a01274e0961ae50c2a4f4498882e3d7eb" title="&lt; Url to the database">dbName</a>,</div>
<div class="line"><a name="l00061"></a><span class="lineno">   61</span>&#160;                <a class="code" href="a00012.html#a9866bb45cacbe65df40aa02f9cfe8329" title="&lt; Java jdbc driver">username</a>, <a class="code" href="a00012.html#add82e06e9083c528e3f1046f3cdfd942" title="&lt; Username wich is used on the database">password</a>);</div>
<div class="line"><a name="l00062"></a><span class="lineno">   62</span>&#160;        <a class="code" href="a00012.html#a1b54b28524696aa33252c2d3981c3815">logger</a>.info(<span class="stringliteral">&quot;Connection made with DB:&quot;</span> + <a class="code" href="a00012.html#a01274e0961ae50c2a4f4498882e3d7eb" title="&lt; Url to the database">dbName</a>);</div>
<div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;    }</div>
<div class="line"><a name="l00064"></a><span class="lineno">   64</span>&#160;</div>
<div class="line"><a name="l00069"></a><span class="lineno"><a class="code" href="a00012.html#aee72809b506fbf549bf30c8093b2731d">   69</a></span>&#160;    <span class="keyword">private</span> <span class="keywordtype">void</span> <a class="code" href="a00012.html#aee72809b506fbf549bf30c8093b2731d" title="Reads the config.properties wich contains the information for the database.">readConfigFile</a>() {</div>
<div class="line"><a name="l00070"></a><span class="lineno">   70</span>&#160;</div>
<div class="line"><a name="l00071"></a><span class="lineno">   71</span>&#160;        Properties prop = <span class="keyword">new</span> Properties();</div>
<div class="line"><a name="l00072"></a><span class="lineno">   72</span>&#160;        <span class="keywordflow">try</span> {</div>
<div class="line"><a name="l00073"></a><span class="lineno">   73</span>&#160;            prop.load(<span class="keyword">new</span> FileInputStream(<a class="code" href="a00012.html#af438e4318b3b86a14500f1a235da6116" title="&lt; log4j used for logging.">CONFIG_FILE</a>));</div>
<div class="line"><a name="l00074"></a><span class="lineno">   74</span>&#160;        } <span class="keywordflow">catch</span> (Exception e) {</div>
<div class="line"><a name="l00075"></a><span class="lineno">   75</span>&#160;            e.printStackTrace();</div>
<div class="line"><a name="l00076"></a><span class="lineno">   76</span>&#160;        }</div>
<div class="line"><a name="l00077"></a><span class="lineno">   77</span>&#160;</div>
<div class="line"><a name="l00078"></a><span class="lineno">   78</span>&#160;        <a class="code" href="a00012.html#a45f4ebd14354ea2e599f17cfd307046f" title="&lt; An instance of the class SqlConnect">url</a> = prop.getProperty(<span class="stringliteral">&quot;dbUrl&quot;</span>);</div>
<div class="line"><a name="l00079"></a><span class="lineno">   79</span>&#160;        <a class="code" href="a00012.html#a01274e0961ae50c2a4f4498882e3d7eb" title="&lt; Url to the database">dbName</a> = prop.getProperty(<span class="stringliteral">&quot;dbName&quot;</span>);</div>
<div class="line"><a name="l00080"></a><span class="lineno">   80</span>&#160;        <a class="code" href="a00012.html#adcb721f63e93506c637a09aac3ac6df6" title="&lt; Name of the database">driver</a> = prop.getProperty(<span class="stringliteral">&quot;dbDriver&quot;</span>);</div>
<div class="line"><a name="l00081"></a><span class="lineno">   81</span>&#160;        <a class="code" href="a00012.html#a9866bb45cacbe65df40aa02f9cfe8329" title="&lt; Java jdbc driver">username</a> = prop.getProperty(<span class="stringliteral">&quot;dbUsername&quot;</span>);</div>
<div class="line"><a name="l00082"></a><span class="lineno">   82</span>&#160;        <a class="code" href="a00012.html#add82e06e9083c528e3f1046f3cdfd942" title="&lt; Username wich is used on the database">password</a> = prop.getProperty(<span class="stringliteral">&quot;dbPassword&quot;</span>);</div>
<div class="line"><a name="l00083"></a><span class="lineno">   83</span>&#160;    }</div>
<div class="line"><a name="l00084"></a><span class="lineno">   84</span>&#160;</div>
<div class="line"><a name="l00094"></a><span class="lineno"><a class="code" href="a00012.html#a8a736343d2dd35ec71ea1832b8e76b81">   94</a></span>&#160;    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">synchronized</span> <a class="code" href="a00012.html" title="SQL Class used to connect to the database and to execute queries.">SqlConnect</a> <a class="code" href="a00012.html#a8a736343d2dd35ec71ea1832b8e76b81" title="Returns a SqlConnect used the get the singleton.">getDbCon</a>()</div>
<div class="line"><a name="l00095"></a><span class="lineno">   95</span>&#160;            throws InstantiationException, IllegalAccessException,</div>
<div class="line"><a name="l00096"></a><span class="lineno">   96</span>&#160;            ClassNotFoundException, SQLException {</div>
<div class="line"><a name="l00097"></a><span class="lineno">   97</span>&#160;        <span class="keywordflow">if</span> (<a class="code" href="a00012.html#ada1f04b176cbc918263439d50e55295a" title="&lt; Prepared statement that will be used to execute on the database">db</a> == null) {</div>
<div class="line"><a name="l00098"></a><span class="lineno">   98</span>&#160;            <a class="code" href="a00012.html#ada1f04b176cbc918263439d50e55295a" title="&lt; Prepared statement that will be used to execute on the database">db</a> = <span class="keyword">new</span> <a class="code" href="a00012.html#a794ac61e7345eb264f667b269be0faf0" title="&lt; Password wich is used on the database">SqlConnect</a>();</div>
<div class="line"><a name="l00099"></a><span class="lineno">   99</span>&#160;        }</div>
<div class="line"><a name="l00100"></a><span class="lineno">  100</span>&#160;        <span class="keywordflow">return</span> <a class="code" href="a00012.html#ada1f04b176cbc918263439d50e55295a" title="&lt; Prepared statement that will be used to execute on the database">db</a>;</div>
<div class="line"><a name="l00101"></a><span class="lineno">  101</span>&#160;</div>
<div class="line"><a name="l00102"></a><span class="lineno">  102</span>&#160;    }</div>
<div class="line"><a name="l00103"></a><span class="lineno">  103</span>&#160;</div>
<div class="line"><a name="l00112"></a><span class="lineno"><a class="code" href="a00012.html#a85df8794cccd55e7fff6149cccafe091">  112</a></span>&#160;    <span class="keyword">public</span> ResultSet <a class="code" href="a00012.html#a85df8794cccd55e7fff6149cccafe091" title="Execute a query wich will be executed in the database.">query</a>(String <a class="code" href="a00012.html#a85df8794cccd55e7fff6149cccafe091" title="Execute a query wich will be executed in the database.">query</a>) <span class="keywordflow">throws</span> SQLException {</div>
<div class="line"><a name="l00113"></a><span class="lineno">  113</span>&#160;        <a class="code" href="a00012.html#a22891c0396ec494ad8184bfa66dc0c5a" title="&lt; The connection to the database">statement</a> = <a class="code" href="a00012.html#ada1f04b176cbc918263439d50e55295a" title="&lt; Prepared statement that will be used to execute on the database">db</a>.<a class="code" href="a00012.html#a14905a855dd0404405bccfa453a27fd2" title="&lt; Location of project properties file">conn</a>.prepareStatement(<a class="code" href="a00012.html#a85df8794cccd55e7fff6149cccafe091" title="Execute a query wich will be executed in the database.">query</a>);</div>
<div class="line"><a name="l00114"></a><span class="lineno">  114</span>&#160;        ResultSet res = <a class="code" href="a00012.html#a22891c0396ec494ad8184bfa66dc0c5a" title="&lt; The connection to the database">statement</a>.executeQuery();</div>
<div class="line"><a name="l00115"></a><span class="lineno">  115</span>&#160;        <span class="keywordflow">return</span> res;</div>
<div class="line"><a name="l00116"></a><span class="lineno">  116</span>&#160;    }</div>
<div class="line"><a name="l00117"></a><span class="lineno">  117</span>&#160;</div>
<div class="line"><a name="l00123"></a><span class="lineno"><a class="code" href="a00012.html#a616ea38aefd850d4a7c7f2d75fa81fc4">  123</a></span>&#160;    <span class="keyword">public</span> ResultSet <a class="code" href="a00012.html#a616ea38aefd850d4a7c7f2d75fa81fc4" title="Used to insert data into the database.">insert</a>(String insertQuery) <span class="keywordflow">throws</span> SQLException {</div>
<div class="line"><a name="l00124"></a><span class="lineno">  124</span>&#160;        <a class="code" href="a00012.html#a22891c0396ec494ad8184bfa66dc0c5a" title="&lt; The connection to the database">statement</a> = <a class="code" href="a00012.html#ada1f04b176cbc918263439d50e55295a" title="&lt; Prepared statement that will be used to execute on the database">db</a>.<a class="code" href="a00012.html#a14905a855dd0404405bccfa453a27fd2" title="&lt; Location of project properties file">conn</a>.prepareStatement(insertQuery,</div>
<div class="line"><a name="l00125"></a><span class="lineno">  125</span>&#160;                Statement.RETURN_GENERATED_KEYS);</div>
<div class="line"><a name="l00126"></a><span class="lineno">  126</span>&#160;        <a class="code" href="a00012.html#a22891c0396ec494ad8184bfa66dc0c5a" title="&lt; The connection to the database">statement</a>.executeUpdate();</div>
<div class="line"><a name="l00127"></a><span class="lineno">  127</span>&#160;</div>
<div class="line"><a name="l00128"></a><span class="lineno">  128</span>&#160;        ResultSet result = <a class="code" href="a00012.html#a22891c0396ec494ad8184bfa66dc0c5a" title="&lt; The connection to the database">statement</a>.getGeneratedKeys();</div>
<div class="line"><a name="l00129"></a><span class="lineno">  129</span>&#160;        <span class="keywordflow">return</span> result;</div>
<div class="line"><a name="l00130"></a><span class="lineno">  130</span>&#160;</div>
<div class="line"><a name="l00131"></a><span class="lineno">  131</span>&#160;    }</div>
<div class="line"><a name="l00132"></a><span class="lineno">  132</span>&#160;}</div>
</div><!-- fragment --></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Feb 1 2013 11:24:57 for SensorFramework by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.2-20121118
</small></address>
</body>
</html>
